import EventCenter from "../../lib/EventCenter";
import EventEunm from "../data/EventEunm";
import GridManager from "../GridManager";

export default class GameTaskScript extends Laya.Script{
    onEnable(){
        this.fresh();
        EventCenter.getIns().on(EventEunm.taskChange,this,this.fresh);
    }
    onDisable(){
        EventCenter.getIns().off(EventEunm.taskChange,this,this.fresh);
    }

    private fresh(){
        if(GridManager.getIns().gateData){
            this.owner.getChildByName("scoreTxt")["text"]=GridManager.getIns().score;
            this.owner.getChildByName("taskTxt")["text"]=GridManager.getIns().getTaskProssStr();
            this.owner.getChildByName("prossImg")["width"]=296*this.getPorss()//GridManager.getIns().getTaskPross();
            this.owner.getChildByName("taskImg")["texture"]=GridManager.getIns().gateData.getTaskIcon();
            this.freshStar();
        }
    }

    private freshStar(){
        let tstar = GridManager.getIns().getTaskStar();
        let tsp;
        for(let i=1;i<=3;i++){
            tsp = this.owner.getChildByName("star"+i);
            tsp["texture"]=tstar>=i?"main/star-enable.png":"main/star-disable.png";
        }
    }

    private getPorss(){
        let tstar = GridManager.getIns().getTaskStar();
        let tpross:number=GridManager.getIns().getTaskPross();
        let tproox = this.owner.getChildByName("prossImg")["x"];
        let tp1 = (this.owner.getChildByName("star1")["x"]-tproox+56)/296;
        let tp2 = (this.owner.getChildByName("star2")["x"]-tproox+56)/296;
        let tp3 = (this.owner.getChildByName("star3")["x"]-tproox+56)/296;
        let tsx:number=this.owner.getChildByName("star1")["x"];
        if(tstar>=3){
            tsx=this.owner.getChildByName("star3")["x"];
        }else if(tstar>=2){
            tsx=this.owner.getChildByName("star2")["x"];
        }
        tsx += 56;
        let tdist = tsx-tproox;
        let tproo2 = tdist/296;
        if(tpross>tproo2)tpross=tproo2;
        if(tstar>=3){
            if(tpross<tp3) tpross=tp3;
        }else if(tstar>=2){
            if(tpross<tp2) tpross=tp2;
        }else if(tstar>=1){
            if(tpross<tp1) tpross=tp1;
        }

        return tpross;
    }

}